<template>
	<view class="content">
		<p>Fvv Wifi Helper</p>
		<button @click="init">初始化</button>
		<button @click="getHost">获取本机ip，mac</button>
		<button @click="scanQRCode">扫码连接</button>
		<button @click="createWifi">开启热点</button>
		<button @click="closeAp">关闭热点</button>
		<button @click="scanWifi">获取wifi列表</button>
		<button @click="connectWifi">连接wifi</button>
		<button @click="staticIP">静态ip</button>
		<button @click="getLAN">获取局域网设备</button>
		<button @click="portScan">端口扫描</button>
		<button @click="getLink">建立连接</button>
		<button @click="offLink">关闭连接</button>
		<button @click="sendMSG">发送消息</button>
		<button @click="getLyccsData">获取心跳</button>
	</view>
</template>

<script>
	import permision from "@/static/permission.js";
	const FvvUniWifiHelper = uni.requireNativePlugin("Fvv-UniWifiHelper");
	const MoeTcpClient = uni.requireNativePlugin("moe-tcp-client");
	export default {
		data() {
			return {

			}
		},
		onLoad() {

		},
		methods: {
			async getPermission() {
				var result = await permision.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION")
				var strStatus
				if (result == 1) {
					strStatus = "已获得授权"
				} else if (result == 0) {
					strStatus = "未获得授权"
				} else {
					strStatus = "被永久拒绝权限"
				}
				uni.showModal({
					content: permisionID + strStatus,
					showCancel: false
				});
			},
			init() {
				FvvUniWifiHelper.init(res => {
					console.log(res)
				})
				this.getPermission()
			},
			getHost() {
				console.log(FvvUniWifiHelper.getHostIP())
				console.log(FvvUniWifiHelper.getHostMac())
			},
			createWifi() {
				this.init();
				console.log(FvvUniWifiHelper.createWifi({
					name: "quanlie_fe009",
					password: "DEF0723G"
				}, res => {
					console.log(res)
				}))
			},
			closeAp() {
				FvvUniWifiHelper.closeWifiAp()
			},
			scanWifi() {
				FvvUniWifiHelper.getWifiList(res => {
					console.log(res)
				})
			},
			connectWifi() {
				FvvUniWifiHelper.connectWifi({
					ssid: "quanlie_fe009",
					password: "DEF0723G"
				});
			},
			staticIP() {
				console.log(FvvUniWifiHelper.setStaticIp({
					dhcp: false,
					ip: "192.168.0.168",
					dns1: "192.168.0.1",
					gateway: "192.168.0.1"
				}));
			},
			// getLAN() {
			// 	console.log("局域网设备")
			// 	FvvUniWifiHelper.getLAN(res => {
			// 		console.log(res)
			// 	})
			// },
			getLAN() {
			  console.log("获取ip");
			  const port = 80; 
			  FvvUniWifiHelper.getLAN(port, res => {
			    console.log('局域网返回结果:', res.ip);
			  });
			},
			// getLAN() {
			//   console.log("获取ip");
			//   // const port = 80; 
			//   FvvUniWifiHelper.getLAN({
			// 		port:"80"
			// 	},res => {
			//     console.log('局域网返回结果:', res);
			//   });
			// },
			portScan() {
				FvvUniWifiHelper.portScan({
					ip: "14.215.177.39",
					port: "80,3306"
				}, res => {
					console.log(res)
				})
			},
			getLink() {
				// console.log("moeTcpClient plugin:", MoeTcpClient);
				MoeTcpClient.connect({
					ip: '192.168.184.253',
					port: 8080
				}, result => {
					console.log(result);
					// 连接结果 
					// {"code":1,"msg":"连接成功","data":""}
					// {"code":0,"msg":"连接失败","data":""}
				})
			},
			offLink() {
				MoeTcpClient.disconnect();
			},
			sendMSG() {
				MoeTcpClient.sendStr({
					message: '发送的内容,hhhh,哈市东方红'
				});
			},
			scanQRCode() { // 扫码并解析账号密码并提示
				uni.scanCode({
					success: (res) => {
						console.log('扫码结果：', res);
						let resultStr = res.result;

						// 替换中文引号
						resultStr = resultStr.replace(/“/g, '"').replace(/”/g, '"');

						// 正则提取
						const ssidMatch = resultStr.match(/S:"(.*?)"/);
						const passwordMatch = resultStr.match(/P:"(.*?)"/);

						if (ssidMatch && passwordMatch) {
							const ssid = ssidMatch[1];
							const password = passwordMatch[1];
							console.log('解析到的SSID:', ssid);
							console.log('解析到的密码:', password);

							// 弹窗提示账号密码
							this.showWifiInfo(ssid, password);

						} else {
							console.error('二维码格式错误，未能解析到SSID和密码');
							uni.showToast({
								title: '解析失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						console.error('扫码失败：', err);
						uni.showToast({
							title: '扫码失败',
							icon: 'none'
						});
					}
				});
			},

			createWifiByScan(ssid, password) {
				console.log("chuangjianwifi")
				FvvUniWifiHelper.createWifi({
					name: ssid,
					password: password
				}, res => {
					console.log('创建热点结果:', res);
					if (res.code === 1) {
						uni.showToast({
							title: '热点创建成功',
							icon: 'success'
						});
					} else {
						uni.showToast({
							title: '创建失败',
							icon: 'none'
						});
					}
				});
			},
			showWifiInfo(ssid, password) {
				console.log("开始复制账号密码");
				uni.showModal({
					title: 'WiFi信息',
					content: `账号：${ssid}\n密码：${password}`,
					confirmText: '复制并去创建',
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							// 先复制账号
							uni.setClipboardData({
								data: ssid,
								success: () => {
									console.log('账号已复制');
									// 再复制密码（覆盖账号）
									uni.setClipboardData({
										data: password,
										success: () => {
											console.log('密码已复制');
											// 复制完密码后跳转WiFi设置
											this.createWifi();
										},
										fail: () => {
											uni.showToast({
												title: '复制密码失败',
												icon: 'none'
											});
										}
									});
								},
								fail: () => {
									uni.showToast({
										title: '复制账号失败',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			},
			getLyccsData() {
			  uni.request({
				url: 'http://192.168.154.93/heartbeat',
				method: 'GET',
				success: (res) => {
				  console.log('接口返回数据：', res);
				  let content = '';
				  if (res.data) {
					content = String(res.data);
				  } else {
					content = '无数据返回';
				  }
				  // 弹窗展示结果
				  uni.showModal({
					title: '心跳：',
					content: content.length > 100 ? content.slice(0, 100) + '...' : content,
					showCancel: false
				  });
				},
				fail: (err) => {
				  console.error('接口请求失败：', err);
				  uni.showToast({
					title: '请求失败',
					icon: 'none'
				  });
				}
			  });
			},







		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>